package com.example.leetcode;

import lombok.extern.slf4j.Slf4j;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: sfx
 * Date: 2023-06-27
 * Time: 15:18
 */
@Slf4j(topic = "c.controlOrder1")
public class controlOrder1 {

    //创建锁对象
    private static final Object lock = new Object();
    private static boolean isRunning;//t2是否运行过了
    public static void main(String[] args) {
        //t1线程打印1,t2线程打印2,保证先打印2在打印1
        Thread t1 = new Thread(() -> {
            synchronized (lock) {
                while (!isRunning) {
                    try {
                        lock.wait();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
                log.debug("1");
            }
        }, "t1");

        Thread t2 = new Thread(() -> {
            synchronized (lock) {
                isRunning = true;
                log.debug("2");
                lock.notify();
            }
        }, "t2");
        Thread.yield();
        t1.start();
        t2.start();
    }
}
